import axios from 'axios'
import { Message } from '@arco-design/web-vue'

// 封装好了一个js端
var client = axios.create({
  baseURL: '',
  timeout: 5000
})

client.interceptors.response.use(
  (response) => {
    return response
  },
  (error) => {
    var msg = error.message
    var code = 0
    if (error.response && error.response.data) {
      msg = error.response.data.message || msg
      code = error.response.data.code
    }

    // 用户Token失效，或者被撤销
    const handleTokenClearAndRedirect = () => {
      location.assign('/login')
    }

    switch (code) {
      case 50000:
      case 50001:
      case 50002:
        handleTokenClearAndRedirect()
        break
      default:
        // 其他处理逻辑
        break
    }

    Message.error(msg)
    return Promise.reject(error)
  }
)

export default client
